﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XiTi
{
    public class PaperParse
    {
        Random random = new Random();
        JObject root;
        List<Question> qs;
        public PaperParse(string json)
        {
            qs = new List<Question>();
            root = JObject.Parse(json);
            JArray content = root["content"] as JArray;
            for(int i=0; i<content.Count; i++)
            {
                JArray questions = content[i]["questions"] as JArray;
                for(int j=0; j<questions.Count; j++)
                {
                    var q = questions[j];
                    Question fq = q.ToObject<Question>();
                    qs.Add(fq);
                }
            }
        }

        public void RandomSortList()
        {
            qs.Sort((Question q1, Question q2) => { return random.Next().CompareTo(random.Next()); });
        }

        public string TakeHtml(DateTime d)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<!DOCTYPE html><html lang='zh'><head><meta charset='UTF-8'><title>每日一题</title></head><body><div>\n");


            for (int i = 0; i < qs.Count; i++)
            {
                Question q = qs[i];

                sb.Append("<div>");
                sb.Append("<h2>").Append(d.ToString("yyyy-MM-dd")).Append("日习题</h2>");
                sb.Append(JsonHelper.question2Html(q));
                sb.Append("</div>");
                sb.Append("<div>这里分页<div>\n");
                d = d.AddDays(1);
            }
            sb.Append("</div></body></html>");
            return sb.ToString();
        }
    }
}
